from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import KeShi, Bottle, AirType, State, Size, Log, Person
from django.template import loader
from .forms import ChangeBottle



# 科室库存盘点
def keshikucun():
    keshi= KeShi.objects.order_by('id')
    info = {}
    for each in keshi:
        info[each.name] = len(Bottle.objects.filter(loc__id=each.id))
        # print(each.name)
    return info

# 气体类型盘点
def qiti_num():
    qiti = AirType.objects.order_by('id')
    info = {}
    for each in qiti:
        info[each.name] = len(Bottle.objects.filter(type__id=each.id))
        # print(each.name)
    return info


# 气瓶状态盘点
def state_num():
    state = State.objects.order_by('id')
    info = {}
    for each in state:
        info[each.name] = len(Bottle.objects.filter(state__id=each.id))
        # print(each.name)
    return info




#####################################################################################
# 数据可视化首页
def index(request):
    template = loader.get_template('airhub/index.html')
    # 表头数据
    bottle_num = Bottle.objects.count()         # 气瓶录入总量
    log_num = Log.objects.count()       # 气瓶配送总量
    # 数据面板
    bottle_O2_num = Bottle.objects.filter(type__id=1).count()                   # 氧气数量
    bottle_40_num = Bottle.objects.filter(size__id=1, type__id=1).count()       # 40L氧气数量
    bottle_10_num = Bottle.objects.filter(size__id=2, type__id=1).count()       # 10L氧气数量
    bottle_CO2_num = Bottle.objects.filter(type__id=2).count()                  # 氮气数量
    bottle_N2_num = Bottle.objects.filter(type__id=3).count()                   # 氮气数量

    # 空满数据占比
    bottle_Man = Bottle.objects.filter(state__id=1).count()                     # 满瓶数量
    bottle_Kong = Bottle.objects.filter(state__id=2).count()                    # 空瓶数量

    # 科室库存占比
    kucun_dic = {}
    kucun_name = []
    kucun_num = []
    keshi = KeShi.objects.filter()
    for each in keshi:
        kucun_dic[each.name]=Bottle.objects.filter(loc__id=each.id).count()
        kucun_name.append(each.name)
        kucun_num.append(Bottle.objects.filter(loc__id=each.id).count())
    # print(str(kucun_dic))
    # print(str(kucun_name))
    # print(str(kucun_num))


    # 新增气瓶列表
    new_bottle = Bottle.objects.order_by('-id')[:6]

    # 新增配送列表
    new_log = Log.objects.order_by('-id')[:6]

    context = {
        'bottle_num': bottle_num,
        'log_num': log_num,
        'bottle_O2_num': bottle_O2_num,
        'bottle_40_num': bottle_40_num,
        'bottle_10_num': bottle_10_num,
        'bottle_CO2_num': bottle_CO2_num,
        'bottle_N2_num': bottle_N2_num,
        'bottle_Man': bottle_Man,
        'bottle_Kong': bottle_Kong,
        'kucun_dic': kucun_dic,
        'kucun_name': str(kucun_name),
        'kucun_num': str(kucun_num),
        'new_bottle': new_bottle,
        'new_log': new_log,

    }
    return HttpResponse(template.render(context, request))





# 气瓶详情页面
def bottleinfo(request, id):       #气瓶详情
    bottle = get_object_or_404(Bottle, id = id)
    info = str(bottle.state) +',' +str(bottle.type)+',' + str(bottle.uid) + ',' + str(bottle.name)
    return HttpResponse(info)


# 科室库存页面
def keshi(request, name):
    list = Bottle.objects.filter(loc__name=name)
    if len(list) == 0:
        return HttpResponse('无库存')
    else:
        INFO = ''
        for each in list:
            INFO = INFO + each.name + ','
        return HttpResponse(INFO)


# 添加气瓶api
def add_bottle_api(request):
    print(request.method)
    if request.method == "POST":
        if request.POST:
            name = request.POST.get('name', 0)
            state = request.POST.get('state', 0)
            uid = request.POST.get('uid', 0)
            type = request.POST.get('type', 0)
            bro_date = request.POST.get('bro_date', 0)
            loc = request.POST.get('loc', 0)
            size = request.POST.get('size', 0)
            if name and state and uid and type and bro_date and size:
                bottle = Bottle()
                bottle.name = name
                bottle.uid = uid
                bottle.state = get_object_or_404(State, id=state)
                bottle.type = get_object_or_404(AirType, id=type)
                bottle.bro_date = bro_date
                bottle.loc = get_object_or_404(KeShi, id=loc)
                bottle.size = get_object_or_404(Size, id=size)
                bottle.save()
                info = name + ',' + uid + ',' + state + ',' + type
                return HttpResponse('修改成功'+',' + info)
            else:
                return HttpResponse('输入错误')
        else:
            return HttpResponse('输入为空')
    else:
        return HttpResponse('方法错误')


# 变更气瓶log_api
def change_bottle_api(request):
    if request.method == 'POST':
        if request.POST :
            state = request.POST.get('state', 0)
            loc = request.POST.get('loc', 0)
            bottle_id = request.POST.get('bottle_id', 0)
            person = request.POST.get('person', 0)
            print(bottle_id, state, loc)
            if state and loc and bottle_id:
                bottle = Bottle.objects.get(id=bottle_id)
                bottle.state = State.objects.get(id=state)
                bottle.loc = KeShi.objects.get(id=loc)
                bottle.save()
                log = Log()
                log.loc = KeShi.objects.get(id=loc)
                log.state = State.objects.get(id=state)
                log.bottle = Bottle.objects.get(id=bottle_id)
                log.person = Person.objects.get(id=person)
                log.save()
                return HttpResponse('修改成功')
            else:
                return HttpResponse('输入为空')
        else:
            return HttpResponse('方法错误')


# show_主页
def show(request):
    template = loader.get_template('airhub/show.html')
    airtype = AirType.objects.all()
    keshiname= KeShi.objects.all()
    context = {
        'airtype': airtype,
        'keshiname': keshiname,
    }
    return HttpResponse(template.render(context, request))


# 气体列表
def show_airtype(request, id):
    template = loader.get_template('airhub/show_airtype.html')
    airtype = AirType.objects.all()
    keshiname= KeShi.objects.all()
    bottle_list = Bottle.objects.filter(type__id=id)
    airtype_name = AirType.objects.get(pk=id).name
    bottle_list_num = Bottle.objects.filter(type__id=id).count()
    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'bottle_list': bottle_list,
        'airtype_name': airtype_name,
        'bottle_list_num': bottle_list_num,
    }
    return HttpResponse(template.render(context, request))

# 科室列表
def show_keshi(request, id):
    template = loader.get_template('airhub/show_keshi.html')
    airtype = AirType.objects.all()
    keshiname= KeShi.objects.all()
    bottle_list = Bottle.objects.filter(loc__id=id)
    keshi_name = KeShi.objects.get(pk=id).name
    bottle_list_num = Bottle.objects.filter(loc__id=id).count()
    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'bottle_list': bottle_list,
        'keshi_name': keshi_name,
        'bottle_list_num': bottle_list_num,
    }
    return HttpResponse(template.render(context, request))

# 气瓶详情
def show_bottle(request, id):
    template = loader.get_template('airhub/show_bottle.html')
    airtype = AirType.objects.all()
    keshiname= KeShi.objects.all()
    bottle = Bottle.objects.get(pk=id)
    bottle_name = Bottle.objects.get(pk=id).name
    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'bottle': bottle,
        'bottle_name': bottle_name,
    }
    return HttpResponse(template.render(context, request))


# 气瓶配送
def change_bottle(request):
    template = loader.get_template('airhub/changebottle.html')
    airtype = AirType.objects.all()
    keshiname= KeShi.objects.all()
    if request.method == 'POST':
        if request.POST:
            state = request.POST.get('state', 0)
            loc = request.POST.get('loc', 0)

            bottle_id = request.POST.get('bottle_id', 0)
            if len(bottle_id) >= 10:
                bottle_id = bottle_id[:-1]
            person = request.POST.get('user_key', 0)
            print(bottle_id, state, loc)
            if state and loc and bottle_id:
                try:
                    bottle = Bottle.objects.get(id=bottle_id)
                    bottle.state = State.objects.get(id=state)
                    bottle.loc = KeShi.objects.get(id=loc)
                    bottle.save()
                    log = Log()
                    log.loc = KeShi.objects.get(id=loc)
                    log.state = State.objects.get(id=state)
                    log.bottle = Bottle.objects.get(id=bottle_id)
                    log.person = Person.objects.get(key=person)
                    log.save()
                    info = '修改成功:      ' + '气瓶ID:' + bottle_id
                    context = {
                        'airtype': airtype,
                        'keshiname': keshiname,
                        'info': info,

                    }
                    return HttpResponse(template.render(context, request))
                except:
                    info = '输入错误'
                    context = {
                        'airtype': airtype,
                        'keshiname': keshiname,
                        'info': info,

                    }
                    return HttpResponse(template.render(context, request))

            else:
                info = '输入为空'
                context = {
                    'airtype': airtype,
                    'keshiname': keshiname,
                    'info': info,

                }
                return HttpResponse(template.render(context, request))

        else:
            context = {
                'airtype': airtype,
                'keshiname': keshiname,

            }
            return HttpResponse(template.render(context, request))
    else:
        info = '配送信息'
        context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'info': info,
        }
        return HttpResponse(template.render(context, request))

def change_bottle(request):
    template = loader.get_template('airhub/changebottle.html')
    airtype = AirType.objects.all()
    keshiname= KeShi.objects.all()
    person = Person.objects.all()
    state = State.objects.all()

    if request.method == "POST":
        if request.POST:
            state = request.POST.get('state', 0)
            loc = request.POST.get('loc', 0)
            bottle_id = request.POST.get('bottle_id', 0)
            person = request.POST.get('user', 0)
            if len(bottle_id) >= 10:
                bottle_id = bottle_id[:-1]

            if state and loc and bottle_id and person:
                try:
                    bottle = Bottle.objects.get(id=bottle_id)
                    bottle.state = State.objects.get(id=state)
                    bottle.loc = KeShi.objects.get(id=loc)
                    bottle.save()

                    log = Log()
                    log.loc = KeShi.objects.get(id=loc)
                    log.state = State.objects.get(id=state)
                    log.bottle = Bottle.objects.get(id=bottle_id)
                    log.person = Person.objects.get(id=person)
                    log.save()

                    info = '修改成功:      ' + '气瓶ID:' + bottle_id

                except:
                    info = '输入错误'
            else:
                info = '输入为空'
        else:
            info = '输入为空'
    else:
        info = '气瓶配送'

    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'person': person,
        'state': state,
        'info': info,
    }
    return HttpResponse(template.render(context, request))


# 新增气瓶
def add_bottle(request):
    template = loader.get_template('airhub/addbottle.html')

    airtype = AirType.objects.all()
    keshiname = KeShi.objects.all()
    airsize = Size.objects.all()


    if request.method == "POST":
        if request.POST:
            name = '气瓶'
            # state = request.POST.get('state', 0)
            type = request.POST.get('type', 0)
            bro_date = request.POST.get('bro_date', 0)
            # loc = request.POST.get('loc', 0)
            size = request.POST.get('size', 0)
            print(type, bro_date, size)
            if type and bro_date and size:
                bottle = Bottle()
                bottle.name = name

                bottle.state = get_object_or_404(State, id=1)
                bottle.type = get_object_or_404(AirType, id=type)
                bottle.bro_date = bro_date
                bottle.loc = get_object_or_404(KeShi, id=1)
                bottle.size = get_object_or_404(Size, id=size)

                bottle.uid = str(Bottle.objects.all().count() + 1)
                bottle.save()

                info = '修改成功'

            else:
                info = '输入错误'

        else:
            info = '输入为空'

    else:
        info = '新增气瓶信息'













    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'airsize': airsize,
        'info': info,

    }
    return HttpResponse(template.render(context, request))



# 气瓶入库
def add(request):
    template = loader.get_template('airhub/add.html')
    airtype = AirType.objects.all()
    keshiname = KeShi.objects.all()

    if request.method == 'POST':
        if request.POST:
            bottle_id = request.POST.get('bottle_id', 0)
            if len(bottle_id) >= 10:
                bottle_id = bottle_id[:-1]
            if bottle_id:
                try:
                    bottle = Bottle.objects.get(id=bottle_id)
                    bottle.loc = KeShi.objects.get(id=1)
                    bottle.state = State.objects.get(id=1)
                    bottle.save()
                    info = '修改成功:      ' + '气瓶ID:' + bottle_id
                except:
                    info = '输入错误'

            else:
                info = '输入为空'



        else:

            info = '输入为空'
    else:
        info = '快速操作:气瓶入库'




    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'info': info,
    }
    return HttpResponse(template.render(context, request))




# 气瓶出库
def push(request):
    template = loader.get_template('airhub/push.html')
    airtype = AirType.objects.all()
    keshiname = KeShi.objects.all()

    if request.method == 'POST':
        if request.POST:
            bottle_id = request.POST.get('bottle_id', 0)
            if len(bottle_id) >= 10:
                bottle_id = bottle_id[:-1]
            if bottle_id:
                try:
                    bottle = Bottle.objects.get(id=bottle_id)
                    bottle.loc = KeShi.objects.get(id=2)
                    bottle.state = State.objects.get(id=2)
                    bottle.save()
                    info = '修改成功:       ' + '气瓶ID:' + bottle_id
                except:
                    info = '输入错误'

            else:
                info = '输入为空'



        else:

            info = '输入为空'
    else:
        info = '快速操作:气瓶出库'




    context = {
        'airtype': airtype,
        'keshiname': keshiname,
        'info': info,
    }
    return HttpResponse(template.render(context, request))



































